package com.nbsaas.boot.cms.rest.resource;

import com.nbsaas.boot.cms.api.apis.NewsCatalogApi;
import com.nbsaas.boot.cms.data.entity.NewsCatalog;
import com.nbsaas.boot.cms.api.domain.request.NewsCatalogRequest;
import com.nbsaas.boot.cms.api.domain.response.NewsCatalogResponse;
import com.nbsaas.boot.cms.api.domain.simple.NewsCatalogSimple;
import com.nbsaas.boot.cms.rest.convert.NewsCatalogSimpleConvert;
import com.nbsaas.boot.cms.rest.convert.NewsCatalogEntityConvert;
import com.nbsaas.boot.cms.rest.convert.NewsCatalogResponseConvert;
import com.nbsaas.boot.cms.data.repository.NewsCatalogRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.request.PageRequest;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.cms.api.domain.request.NewsCatalogSearch;
import com.nbsaas.boot.rest.utils.TreeUtils;
import java.util.List;
/**
*   新闻分类-业务接口实现
*/
@Transactional
@Service
public class NewsCatalogResource extends BaseResource<NewsCatalog,NewsCatalogResponse, NewsCatalogSimple, NewsCatalogRequest>  implements NewsCatalogApi {

    @Resource
    private NewsCatalogRepository newsCatalogRepository;

    @Override
    public JpaRepositoryImplementation<NewsCatalog, Serializable> getJpaRepository() {
        return newsCatalogRepository;
    }

    @Override
    public Function<NewsCatalog, NewsCatalogSimple> getConvertSimple() {
        return new NewsCatalogSimpleConvert();
    }

    @Override
    public Function<NewsCatalogRequest, NewsCatalog> getConvertForm() {
        return new NewsCatalogEntityConvert();
    }

    @Override
    public Function<NewsCatalog, NewsCatalogResponse> getConvertResponse() {
        return new NewsCatalogResponseConvert();
    }



    @Transactional(readOnly = true)
    @Override
    public ListResponse<NewsCatalogSimple> list(PageRequest request) {
        NewsCatalogSimpleConvert convert=new NewsCatalogSimpleConvert();
        if (request instanceof NewsCatalogSearch){
             NewsCatalogSearch searchRequest=(NewsCatalogSearch)request;
             convert.setFetch(searchRequest.getFetch());
        }
        return listSimple(request,convert);
    }


    @Transactional(readOnly = true)
    @Override
    public ListResponse<NewsCatalogSimple> root(NewsCatalogSearch search) {
        ListResponse<NewsCatalogSimple> result=new  ListResponse<>();
        List<NewsCatalogSimple> simples = list(search).getData();
        result.setData(TreeUtils.tree(simples));
        return result;
    }


}


